﻿using _2112A_2_MES._XiangHe;
using _2112A_2_MES.XiangHes;
using _2112A_2_MES.XiangHes.Measuress;
using _2112A_2_MES.XiangHes.Unities;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace _2112A_2_MES.XiangHe.Measuress
{
    [ApiExplorerSettings(GroupName = "项鹤")]
    public class MeasuresService : ApplicationService, IMeasureService
    {
        private readonly IRepository<Measures, int> _mearepository;
        public MeasuresService(IRepository<Measures, int> mearepository)
        {
            _mearepository = mearepository;
        }

        /// <summary>
        /// 获取计量单位列表
        /// </summary>
        /// <param name="meaCode"></param>
        /// <param name="meaName"></param>
        /// <returns></returns>
        [HttpGet, Route("/XiangHe/GetMeasureList")]
        public async Task<ReturnInfo<List<Measures>>> GetMeasureList(string? meaCode, string? meaName, int pageIndex, int pageSize)
        {
            var meaList = await _mearepository.GetListAsync();
            if (!string.IsNullOrEmpty(meaCode))
            {
                meaList = meaList.Where(x => x.MeasureCode.Contains(meaCode)).ToList();
            }
            if (!string.IsNullOrEmpty(meaName))
            {
                meaList = meaList.Where(x => x.MeasureName.Contains(meaName)).ToList();
            }
            int totalCount = meaList.Count();
            meaList = meaList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            //var data = new { totalCount, meaList };
            if (meaList != null)
            {
                return new ReturnInfo<List<Measures>>(true, "0", meaList, totalCount);
            }

            return new ReturnInfo<List<Measures>>(false, "1", null, 0);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="measure"></param>
        /// <returns></returns>
        [HttpGet, Route("/XiangHe/AddMeasure")]
        public async Task<int> AddMeasure(Measures measure)
        {
            measure.CreateBy = "项鹤";
            measure.CreateTime = DateTime.Now;
            //添加重复
            var list = await _mearepository.GetListAsync(x => x.MeasureCode == measure.MeasureCode || x.MeasureName == measure.MeasureName);
            if (list.Count() > 0)
            {
                return -1;
            }
            await _mearepository.InsertAsync(measure);
            return 1;
        }

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("/XiangHe/GetSingleMea")]
        public async Task<Measures> GetSingleMea(int id)
        {
            var list = await _mearepository.GetAsync(id);
            return list;
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="measure"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("/XiangHe/UpdateMeasure")]
        public async Task<int> UpdateMeasure(Measures measure, int id)
        {
            var list = await _mearepository.GetAsync(id);
            list.MeasureCode = measure.MeasureCode;
            list.MeasureName = measure.MeasureName;
            list.PrimaryFlag = measure.PrimaryFlag;
            list.PrimaryId = measure.PrimaryId;
            list.ChangeRate = measure.ChangeRate;
            list.EnableFlag = measure.EnableFlag;
            list.Remark = measure.Remark;
            list.CreateBy = measure.CreateBy;
            list.CreateTime = measure.CreateTime;
            list.UpdateBy = measure.CreateBy;
            list.UpdateTime = DateTime.Now;
            await _mearepository.UpdateAsync(list);
            return 1;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("/XiangHe/DeleteMeasure")]
        public async Task<int> DeleteMeasure(int id)
        {
            await _mearepository.DeleteAsync(id);
            return 1;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("/XiangHe/DeleteRangeMeasure")]
        public async Task<int> DeleteRangeMeasure(string id)
        {
            var array = id.Split(',');
            var list = await _mearepository.GetListAsync();
            var input = list.Where(m => array.Contains(m.Id.ToString()));
            await _mearepository.DeleteManyAsync(input);
            return 1;
        }
    }
}
